﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Engine.MarchCube
{
    public enum ScalarType
    {
        None=0,
        Sphere=1,
        Cube=2,
        Test = 3

    }
    public static class ScalarFunction
    {
        public static float Function(ScalarType typeElement, Vector3 center, Vector3 checkPos, float Size)
        {
            float result=0;
            float dist ; 
            float xc, yc, zc;
                   xc = (checkPos.X - center.X);
                      yc = (checkPos.Y - center.Y);
                      zc = (checkPos.Z - center.Z); 

                    Vector3 Min =center-new Vector3(Size/2);
                    Vector3 Max = center + new Vector3(Size / 2);
            //= Vector3.Distance(center, checkPos);
            switch (typeElement)
            {
                case ScalarType.None:
                    result = 0;
                    break;
                case ScalarType.Sphere:
                   // dist = Vector3.DistanceSquared(center, checkPos);
                     
                    result =(Size/2f)/(float)Math.Sqrt((xc*xc+yc*yc+zc*zc)) ;
                    break;
                case ScalarType.Cube:
                   
    if (checkPos.X >= Min.X && checkPos.X <= Max.X &&
        checkPos.Y >= Min.Y && checkPos.Y <= Max.Y &&
        checkPos.Z >= Min.Z && checkPos.Z <= Max.Z)
    {
        result = Size;
    }
    else
    {
        result = Size-Vector3.Distance(center, checkPos) ;
    }
                //  Vector3  tV = Vector3.Subtract(center, checkPos);
                //  result = (float)(Size/ (Math.Pow(tV.X, 100d) + Math.Pow(tV.Y, 100d) + Math.Pow(tV.Z, 100d)));
                    break;
                case ScalarType.Test:
                    Matrix r = Matrix.CreateTranslation(center);

                    //result = r.M11 * checkPos.X * checkPos.X
                    //    + r.M22 * checkPos.Y * checkPos.Y
                    //    + r.M33 * checkPos.Z * checkPos.Z
                    //    + r.M12*checkPos.X*checkPos.Y
                    //    + r.M13 * checkPos.X * checkPos.Z
                    //    + r.M23 * checkPos.Y * checkPos.Z
                    //    + r.M14*checkPos.X
                    //    + r.M24 * checkPos.Y
                    //    + r.M34 * checkPos.Z
                    //    + r.M44
                    //    + ((0.2f) / (float)Math.Sqrt((xc * xc + yc * yc + zc * zc)));
                    result = r.M11 * xc * xc
                              + r.M22 * yc * yc
                              + r.M33 * zc * zc
                              + r.M12 * xc * yc
                              + r.M13 * xc * zc
                              + r.M23 * yc * zc
                              + r.M14 * xc
                              + r.M24 * yc
                              + r.M34 * zc
                              + r.M44;
                    r = Matrix.CreateTranslation(checkPos);
                    result += r.M11 * xc * xc
                              + r.M22 * yc * yc
                              + r.M33 * zc * zc
                              + r.M12 * xc * yc
                              + r.M13 * xc * zc
                              + r.M23 * yc * zc
                              + r.M14 * xc
                              + r.M24 * yc
                              + r.M34 * zc
                              + r.M44;
                             

    
                    break;
                    
                default:
                    break;
            }
            return result;
        }
    }
}
